using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
using System.Linq;
using System.Net.WebSockets;
using Ceri.Platform.LES.Dao.Tools;
using Microsoft.Extensions.Configuration;
using Civic.Platform.Teach.Model.Common;

namespace Civic.Platform.Teach.Service.API

{
    public  class WebSocketHelper
    {
        private static List<Teach_WEBSOCKET> clientPool = new List<Teach_WEBSOCKET>();
        private static List<Teach_WEBSOCKET_MSG> Message = new List<Teach_WEBSOCKET_MSG>();
        private static Boolean isClear = true;
        
        
        public void Start()
        {            
            Boardcast();           
        }

        public static List<Teach_WEBSOCKET> GetClientPool()
        {
            return clientPool;
        }
        public static void AddClientPool(Teach_WEBSOCKET lesWebsocket) 
        {
            clientPool.Add(lesWebsocket);
        }

        public static void RemoveClientPool(Teach_WEBSOCKET lesWebsocket)
        {
            clientPool.Remove(lesWebsocket);
        }

        public static void AddMessage(Teach_WEBSOCKET_MSG lesWebsocketMsg)
        {
            Message.Add(lesWebsocketMsg);
        }


        //推送消息
        public static void PushMessage(string form, string context)
        {
            //Console.WriteLine("Push : {0}", context);
            Message.Add(new Teach_WEBSOCKET_MSG { TypeName = form,Msg = context });
            isClear = false;
        }

        private void Boardcast()
        {
            Thread boardcaseThread = new Thread(() =>
            {
                while (true)
                {
                    if (!isClear)
                    {
                        try
                        {
                            Dictionary<int, int> dict = new Dictionary<int, int>();
                            
                            byte[] tmp = Encoding.UTF8.GetBytes(Message[0].Msg);
                            foreach (Teach_WEBSOCKET node in clientPool)
                            {
                                if (node.TypeName == Message[0].TypeName)
                                {
                                    WebSocket client = node.Socket;
                                    if (client.State == WebSocketState.Open)
                                    {
                                        client.SendAsync(new ArraySegment<byte>(tmp, 0, tmp.Length), node.MessageType, node.EndOfMessage, CancellationToken.None);
                                    }
                                    else
                                    {
                                        clientPool.Remove(node);
                                    }
                                    
                                }                               
                            }
                            Message.RemoveAt(0);
                            isClear = Message.Count > 0 ? false : true;
                        }
                        catch (Exception)
                        {

                           
                        }
                        
                    }
                }
            });

            boardcaseThread.Start();
        }
       
    }
}

